<!DOCTYPE html>
<html>
<head>
    <style>
        .switch-container {
            display: inline-flex;
            align-items: center;
            gap: 10px;
        }

        .switch {
            position: relative;
            display: inline-block;
            width: 48px;
            height: 24px;
        }

        .switch input {
            opacity: 0;
            width: 0;
            height: 0;
        }

        .slider {
            position: absolute;
            cursor: pointer;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background-color: #ccc;
            transition: .4s;
            border-radius: 12px;
        }

        .slider:before {
            position: absolute;
            content: "";
            height: 20px;
            width: 20px;
            left: 2px;
            bottom: 2px;
            background-color: white;
            transition: .4s;
            border-radius: 50%;
        }

        input:checked + .slider {
            background-color: #2196F3;
        }

        input:checked + .slider:before {
            transform: translateX(24px);
        }

        /* Optional: Hover effects */
        .slider:hover {
            box-shadow: 0 0 1px #2196F3;
        }

        /* 标签样式 */
        .switch-label {
            font-size: 16px;
            color: #333;
            user-select: none;
            height: 100%;
        }
    </style>
</head>
<body>
    <div class="switch-container">
        <label class="switch">
            <input type="checkbox" id="setting">
            <span class="slider"></span>
        </label>
        <label class="switch-label" for="setting">关</span>
    </div>
    <button style="height: 24px;vertical-align:middle">设置</button>

    <script>
        const switchInput = document.querySelector('input[type="checkbox"]');
        const switchLabel = document.querySelector('.switch-label');

        // 更新标签内容
        const updateLabel = () => {
            switchLabel.textContent = switchInput.checked ? '开' : '关';
            switchLabel.style.color = switchInput.checked ? '#2196F3' : '#333';
        };

        // 初始化标签
        updateLabel();

        // 添加开关状态变化事件
        switchInput.addEventListener('change', updateLabel);

        // 为了支持移动端，添加触摸事件
        document.querySelector('.switch').addEventListener('touchstart', function(e) {
            e.preventDefault();
            this.querySelector('input').click();
        });
    </script>
</body>
</html>